﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Director.DomainModels;

namespace Director.ViewModels
{
	class StrategyResultsVM:AbstractDocVM
	{
		private int _candleWidth;
		private Image _image;
		private WriteableBitmap _bitmap;
		private int _position;
		private Candle[] _candles;

		public ImageSource ImageSource
		{
			get { return _bitmap; }
		}

		public int Maximum
		{
			get
			{
				var maximum = _candles.Length - ImageActualWidth / _candleWidth + 10;
				if (_position > maximum)
				{
					Position = maximum;
					OnPropertyChanged(() => Position);
				}
				return maximum;
			}
		}

		public Image Image
		{
			get { return _image; }
			set
			{
				_image = value;
				_image.SizeChanged += Resize;
				_image.MouseMove += MouseMove;
				_image.MouseWheel += OnMouseWheel;
			}
		}

		public int ImageActualWidth
		{
			get { return _image == null ? 0 : (int)_image.ActualWidth; }
		}

		private void MouseMove(object sender, MouseEventArgs e)
		{
			MousePosition = e.GetPosition(Image);
			Draw();
		}

		private void Draw()
		{
			throw new NotImplementedException();
		}

		public Point MousePosition { get; set; }

		public int Position
		{
			get { return _position; }
			set
			{
				_position = value;
				Draw();
			}
		}

		public int CandleWidth
		{
			get { return _candleWidth; }
			set
			{
				_candleWidth = value;
				OnPropertyChanged(() => Maximum);
				Draw();
			}
		}

		private void Resize(object sender, SizeChangedEventArgs sizeChangedEventArgs)
		{
			OnPropertyChanged(() => ImageActualWidth);
			OnPropertyChanged(() => Maximum);
			Draw();
		}

		void OnMouseWheel(object sender, MouseWheelEventArgs e)
		{
			var v = CandleWidth + Math.Sign(e.Delta);
			if (v < 1) v = 1;
			else if (v > 20) v = 20;
			CandleWidth = v;
			OnPropertyChanged(() => CandleWidth);
		}

		public StrategyResultsVM(MainVM mainVM) : base(mainVM)
		{
		}

		public override string Title
		{
			get { return "Results"; }
		}

		public override string ContentId
		{
			get { throw new NotImplementedException(); }
		}

		public override void Init()
		{
			throw new NotImplementedException();
		}

		public override EDocId DocId
		{
			get { throw new NotImplementedException(); }
		}

		public override void CandlesUpdated()
		{
			throw new NotImplementedException();
		}
	}
}
